package sk.stuba.fiit.ltasys.web.mvc.model.entity;

import java.math.BigInteger;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * CERTIFICATE Entity.
 * 
 * @author Marek Hruska
 */
@Entity
@Table(name = "CERTIFICATE")
public class CertificateEntity {
	
	public static final transient String SERIAL_NUMBER = "serialNumber";
	public static final transient String ISSUER = "issuer";
	public static final transient String SUBJECT = "subject";
	

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer cert_id = null;
	private BigInteger serialNumber = null;
	private Date valid_from = null;
	private Date valid_to = null;
	private byte[] cert = null;
	private String issuer = null;
	private String subject = null;
	private Boolean trustedCert = false;
	
	
	public void convertToCertificateEntity(X509Certificate certificate) throws CertificateEncodingException{
		this.serialNumber = certificate.getSerialNumber();
		this.valid_from = certificate.getNotBefore();
		this.valid_to = certificate.getNotAfter();
		this.issuer = certificate.getIssuerDN().toString();
		this.cert = certificate.getEncoded();
		this.subject = certificate.getSubjectDN().toString();
	}
	
	public CertificateEntity() {}

	
	public Integer getCert_id() {
		return cert_id;
	}


	public void setCert_id(Integer cert_id) {
		this.cert_id = cert_id;
	}


	public BigInteger getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(BigInteger serialNumber) {
		this.serialNumber = serialNumber;
	}

	public Date getValid_from() {
		return valid_from;
	}

	public void setValid_from(Date valid_from) {
		this.valid_from = valid_from;
	}

	public Date getValid_to() {
		return valid_to;
	}

	public void setValid_to(Date valid_to) {
		this.valid_to = valid_to;
	}

	public byte[] getCert() {
		return cert;
	}

	public void setCert(byte[] cert) {
		this.cert = cert;
	}

	public String getIssuer() {
		return issuer;
	}

	public void setIssuer(String issuer) {
		this.issuer = issuer;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(cert);
		result = prime * result + ((cert_id == null) ? 0 : cert_id.hashCode());
		result = prime * result + ((issuer == null) ? 0 : issuer.hashCode());
		result = prime * result
				+ ((serialNumber == null) ? 0 : serialNumber.hashCode());
		result = prime * result
				+ ((valid_from == null) ? 0 : valid_from.hashCode());
		result = prime * result
				+ ((valid_to == null) ? 0 : valid_to.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CertificateEntity other = (CertificateEntity) obj;
		if (!Arrays.equals(cert, other.cert))
			return false;
		if (cert_id == null) {
			if (other.cert_id != null)
				return false;
		} else if (!cert_id.equals(other.cert_id))
			return false;
		if (issuer == null) {
			if (other.issuer != null)
				return false;
		} else if (!issuer.equals(other.issuer))
			return false;
		if (serialNumber == null) {
			if (other.serialNumber != null)
				return false;
		} else if (!serialNumber.equals(other.serialNumber))
			return false;
		if (valid_from == null) {
			if (other.valid_from != null)
				return false;
		} else if (!valid_from.equals(other.valid_from))
			return false;
		if (valid_to == null) {
			if (other.valid_to != null)
				return false;
		} else if (!valid_to.equals(other.valid_to))
			return false;
		return true;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public Boolean isTrustedCert() {
		return trustedCert;
	}

	public void setTrustedCert(Boolean trustedCert) {
		this.trustedCert = trustedCert;
	}
	
	
}
